#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author:余振新
@file: driver_config.py
@time: 2023/03/30
"""

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.firefox.service import Service as FireFoxService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium.webdriver import ChromeOptions
from selenium.webdriver import EdgeOptions
from selenium.webdriver import FirefoxOptions

from common.gol import set_value
from common.log import INFO
from common.read_ini import ReadIni


class BaseDriver:
    def __init__(self):
        self.driver = None
        self.config = ReadIni("/config/config.ini")
        self.url = self.config.read_ini("ui", "url")
        self.browser = self.config.read_ini("ui", "browser").lower()
        self.headless = self.config.read_ini("ui", "headless").lower()
        self.cache = self.config.read_ini("ui", "cache").lower()
        self.cache_path = self.config.read_ini("ui", "cache_path")
        self.browser = self.browser if self.browser in ["chrome", "edge", "firefox"] else "edge"
        if self.headless != "true":
            self.headless = ""
        if self.cache != "true":
            self.cache = ""

    def get_driver(self, _url=None, _browser=None, _cache=None):
        _url = _url or self.url
        _browser = _browser or self.browser
        _cache = _cache or self.cache
        if _browser == "firefox":
            # firefoxdriver_path = ensure_path_sep("/browserdriver/firefox_driver/geckodriver.exe")
            # service = ChromeService(executable_path=firefoxdriver_path)
            INFO.logger.info("使用FireFox浏览器开始测试")
            service = FireFoxService(executable_path=GeckoDriverManager().install())
            options = FirefoxOptions()
            if bool(self.headless):
                options.add_argument('-headless')
            if bool(_cache) and self.cache_path != "":
                options.add_argument("--user-data-dir=" + self.cache_path)
            self.driver = webdriver.Firefox(service=service, options=options)

        elif _browser == "chrome":
            # chromedriver_path = ensure_path_sep("/browserdriver/chrome_driver/chromedriver.exe")
            # service = ChromeService(executable_path=chromedriver_path)
            INFO.logger.info("使用Chrome浏览器开始测试")
            service = ChromeService(executable_path=ChromeDriverManager().install())
            options = ChromeOptions()
            if bool(self.headless):
                options.add_argument('-headless')
            if bool(_cache) and self.cache_path != "":
                options.add_argument("--user-data-dir=" + self.cache_path)
            self.driver = webdriver.Chrome(service=service, options=options)

        elif _browser == "edge":
            INFO.logger.info("使用Edge浏览器开始测试")
            service = EdgeService(executable_path=EdgeChromiumDriverManager().install())
            INFO.logger.info("下载驱动")
            options = EdgeOptions()
            if bool(self.headless):
                options.add_argument('-headless')
            if bool(_cache) and self.cache_path != "":
                options.add_argument("--user-data-dir=" + self.cache_path)
            self.driver = webdriver.Edge(service=service, options=options)
        set_value("driver", self.driver)
        INFO.logger.info("创建驱动对象成功--打开网址--设置全屏")
        self.driver.get(_url)
        self.driver.maximize_window()
        return self.driver


# 进行单元测试
if __name__ == '__main__':
    # BaseDriver().get_driver(_url="https://test-msp.cxjk.com/#/base_home")
    BaseDriver().get_driver()
